/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 20:36:39
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-04-10 09:27:09
 * @FilePath:
 * /xict_ws/src/xcmg-intelligent-cockpit-three/src/xict_ui/include/xict_ui_driver/utils/basecansdk.h
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#ifndef BASECANSDK_H
#define BASECANSDK_H

#include "xict_proto/sensor/can.pb.h"
#include <QMetaType>
#include <QObject>
#include <QThread>
#include <ecal/ecal.h>
#include <ecal/msg/protobuf/subscriber.h>

namespace xict_ui_driver {

    template <typename T> class BaseCanSdk {
    public:
        typedef std::function<void(const T& can_msg)> baseCanCallback;

    public:
        explicit BaseCanSdk(std::string topic) : m_can_topic(topic) { }

        void enableCanSdk() {
            m_can_sub = new eCAL::protobuf::CSubscriber<T>(m_can_topic);
            //        m_can_sub->AddReceiveCallback(std::bind(&m_can_callback,
            //        this, std::placeholders::_1, std::placeholders::_2));
        }

        void disableCanSdk() {
            m_can_sub->Destroy();
        }

        bool setCanCallback(const baseCanCallback& cb) {
            if (cb == nullptr)
                return false;
            m_can_callback = cb;
        }

    private:
        std::string m_can_topic;
        eCAL::protobuf::CSubscriber<T>* m_can_sub;
        baseCanCallback m_can_callback;
    };

    class CanSdk : public QObject {
        Q_OBJECT
    public:
        explicit CanSdk(QObject* parent = nullptr);
        void EnableSdk(int vehicle_type);
        void DisableSdk();
        void GraderCanCallback(const char*, const xcmg_proto::Grader_CAN&);
        void LoaderCanCallback(const char*, const xcmg_proto::Loader_CAN&);
        void ScraperCanCallback(const char*, const xcmg_proto::Scraper_CAN&);
    signals:
        void signalGraderCan(const xcmg_proto::Grader_CAN&);
        void signalLoaderCan(const xcmg_proto::Loader_CAN&);
        void signalScraperCan(const xcmg_proto::Scraper_CAN&);

    private:
        eCAL::protobuf::CSubscriber<xcmg_proto::Grader_CAN> m_grader_can_sub;
        eCAL::protobuf::CSubscriber<xcmg_proto::Loader_CAN> m_loader_can_sub;
        eCAL::protobuf::CSubscriber<xcmg_proto::Scraper_CAN> m_scraper_can_sub;
        eCAL::protobuf::CSubscriber<xcmg_proto::IMU> m_imu_sub;
        int m_current_vehicle_type = 0;
    };

}   // namespace xict_ui_driver

#endif   // BASECANSDK_H
